var searchmenu_speedScale = 0.5;
var searchmenu_direction =  1;

function searchmenu_getcontainer(searchmenu_instanceid)
{
	var searchmenu_instance = document.getElementById(searchmenu_instanceid);
	if(searchmenu_instance) {
		for(var i = 0; i < searchmenu_instance.childNodes.length; i++) {
			if("searchmenu_container" == searchmenu_instance.childNodes[i].id)
				return searchmenu_instance.childNodes[i];
		}
	}
	return null;
}

function sitesearch_openUpdate(sitesearch_instanceid, open_direction)
{

	var sitesearch_container = searchmenu_getcontainer(sitesearch_instanceid);	

	if(sitesearch_container) {
		if(open_direction > 0) {
			if(parseInt(sitesearch_container.style.top) < 0) {
				var topInt = parseInt(sitesearch_container.style.top);
				var topInc = (topInt - (0.5 * topInt * searchmenu_speedScale));
				sitesearch_container.style.top = (topInc > 0 ? 0 : topInc) + "px";
				setTimeout('sitesearch_openUpdate(\'' + sitesearch_instanceid + '\',\'' + open_direction + '\')', 15);
			}
			else
				searchmenu_direction = -1;
		}
		else if(open_direction < 0) {	
			if(parseInt(sitesearch_container.style.top) > -parseInt(sitesearch_container.style.height)) {
				
				var topInc = parseInt(sitesearch_container.style.top) == 0 ? 1 : parseInt(sitesearch_container.style.top) * 2.0;
				sitesearch_container.style.top = -Math.abs(topInc) + "px";
				setTimeout('sitesearch_openUpdate(\'' + sitesearch_instanceid + '\',\'' + open_direction + '\')', 15);
			}
			else
				searchmenu_direction = 1;
		}
	}
}

function sitesearch_onMouseClick(sitesearch_instance)
{
	var activator_this = sitesearch_instance._divActivator;
	var sitesearch_instanceid = (activator_this.id) + "_sitesearch_div";
	
	var sitesearch_instance_div = document.getElementById(sitesearch_instanceid);
	if(sitesearch_instance_div == null) {
	
		sitesearch_instance_div = document.createElement("div");
		sitesearch_instance_div.innerHTML = document.getElementById("searchmenu_template").innerHTML;
		sitesearch_instance_div.id = sitesearch_instanceid;
		activator_this.insertBefore(sitesearch_instance_div, activator_this.firstChild);//Ensures the menumap isnt vertically offset from the activator
		
		var sitesearch_instance_container_style = searchmenu_getcontainer(sitesearch_instanceid).style; 
		var sitesearch_instance_style = sitesearch_instance_div.style;
		var activator_this_style = activator_this.style;
		
		var x_pos = (activator_this_style.left ? parseInt(activator_this_style.left) : 0);
		var y_pos = (activator_this_style.top ? parseInt(activator_this_style.top) : 0) + (activator_this_style.height ? parseInt(activator_this_style.height) : 0);
	 
		sitesearch_instance_style.overflow = "hidden";
		sitesearch_instance_style.visibility = "visible";
		sitesearch_instance_style.left = x_pos + "px";
		sitesearch_instance_style.top = y_pos + "px";	
		sitesearch_instance_style.position = "absolute";
		sitesearch_instance_style.width = sitesearch_instance_container_style.width;
		sitesearch_instance_style.height = sitesearch_instance_container_style.height;
		sitesearch_instance_style.zIndex = 1;
	
		sitesearch_instance_container_style.top = -parseInt(sitesearch_instance_style.height) + "px";
	}
	
if(searchmenu_direction < 0)
		activator_this.style.backgroundImage = "url(" + sitesearch_instance._idleImageURL + ")";
else if(searchmenu_direction > 0)
		activator_this.style.backgroundImage = "url(" + sitesearch_instance._overImageURL + ")";
		setTimeout('sitesearch_openUpdate(\'' + sitesearch_instanceid + '\',\'' + searchmenu_direction + '\')', 33);
		
		
}


function sitesearch_onMouseOut()
{
	var activator_this = document.getElementById(this.id);
	var sitesearch_instanceid = (activator_this.id) + "_sitesearch_div";
	setTimeout('sitesearch_openUpdate(\'' + sitesearch_instanceid + '\',\'' + -1 + '\')', 33);
	
}

function sitesearch(divActivator, idleImageURL, overImageURL)
{
	this._idleImageURL = idleImageURL;
	this._overImageURL = overImageURL;	
	this._divActivator = divActivator;
	this._divActivator.onclick = ( function(n){ return function(){sitesearch_onMouseClick(n);} } )( this );

	var activatorStyle = this._divActivator.style;
	activatorStyle.backgroundImage = "url(" + idleImageURL + ")";
	activatorStyle.backgroundRepeat = "no-repeat";

}